<template>
	<view class="container padding-top">
		<view class="margin-left-sm margin-right-sm margin-bottom bg-white radius10 padding flex  flex-direction">

			<view style="color: #000;">提现金额</view>
			<view class="flex align-center write">
				<text class="cash-icon">¥</text>
				<input v-model="amount" @input='handCharge' type="number" placeholder="请输入提现金额">
			</view>
			<view class="money flex justify-between text-999">
				<view class="money">可用余额 ¥{{ parseFloat(userInfo.balance/ 100).toFixed(2)}}</view>
				<view class="money">手续费 ¥{{charge||0}}</view>
			</view>

		</view>
		<view class="btn" @click="submit">提现</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				rate: '',
				charge: '',
				balance: '',
				userInfo: {},
				amount: '',
				type: ''

			}
		},

		onShow() {
			console.log(uni.getStorageSync('userId'))
			this.getRate()
			this.getUser()
		},
		methods: {
			async getRate() {
				const data = {
					userId: uni.getStorageSync('userId')
				}

				const [err, rs] = await this.$get(this.$api.getChargeRate, data)
				if (rs) {
					this.rate = rs.data
				}
			},
			async getUser() {
				const [err, rs] = await this.$get(this.$api.userDetail)
				if (rs) {
					this.userInfo = rs.data
				}
			},
			submit() {

				if (parseFloat(this.userInfo.balance / 100) < this.amount) {
					return this.$util.msg('提现余额大于可用余额')
				}
				const regex = /^([0-9]+(\.[0-9])?)$/
				console.log(regex.test(this.amount))
				if (!regex.test(this.amount)) {
					return this.$util.msg('请输入正整数或者保留一位小数')
				}
				uni.showModal({
					content: '你确定提现吗',
					success: res => {
						if (res.confirm) {

							// #ifdef  MP-WEIXIN
							const data = {
								appId: this.$config.wxAppId,
								openId: uni.getStorageSync('openid'),
								type: 2,
								userId: uni.getStorageSync('userId'),
								amount: this.amount * 100
							};
							// #endif
							
							// #ifdef  APP-PLUS
							const data = {
								type: 2,
								userId: uni.getStorageSync('userId'),
								amount: this.amount * 100,
								appId: this.$config.appId,
								openId:uni.getStorageSync('openid')
							};
							// #endif

                              console.log(data)

							this.$jsonPost(this.$api.recharge, data).then(res => {
								const [err, rs] = res
								if (rs) {
									this.$util.msg(rs.message)
									this.getUser()
								}

							})
						} else if (res.cancel) {
							console.log('用户点击取消');
						}
					}
				});

			},
			handCharge() {
				const regex = /^([0-9]+(\.[0-9])?)$/
				console.log(regex.test(this.amount))
				if (regex.test(this.amount)) {
					this.charge = this.rate * this.amount
					if (this.charge <= 1) {
						this.charge = 1
					}
					this.charge = parseFloat(this.charge).toFixed(2)
				}

				if (this.amount == '') {
					this.charge = 0
				}




			}
		}
	}
</script>

<style scoped>
	.cash-icon {
		font-size: 54rpx;
		font-weight: 500;
		color: #000;
	}

	.write {
		padding: 60rpx 0 40rpx;
		border-bottom: 2rpx solid #eee;
	}

	input {
		margin-left: 40rpx;
	}

	.money {
		margin-top: 30rpx;
		font-size: 30rpx;
	}

	.btn {
		margin: 80rpx 60rpx 60rpx;
		background: #ED7200;
		border-radius: 20rpx;
		text-align: center;
		height: 80rpx;
		line-height: 80rpx;
		color: #fff;
	}
</style>
